package main

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

var (
	Qywechat    = flag.String("W", "https://qyapi.weixin.qq.com", "企业微信地址，可以是代理,可以加上端口")
	port        = flag.String("P", "5011", "server port ")
	debugMode   = flag.Bool("d", false, "debug mode")
	Accuracy    = flag.String("A", "100", "Value四舍五入的精度")
	SmsURL      = flag.String("smsurl", "http://test.sms.com:8080", "发送短信的服务地址")
	Smsparamkey = flag.String("smskey", "appId,timestamp,sign", "发送短信的post 参数")
	Content     = flag.String("content", "content", "发送短信的post 参数:数据部分 ")
	CellPhone   = flag.String("cellphone", "mobiles", "发送短信的post 参数:手机KEY参数 ")
)

func postserver(c *gin.Context) {
	//agent_id 获取POST参数
	agentid, _ := strconv.Atoi(c.Request.FormValue("agent_id"))
	corpid := c.Request.FormValue("corp_id")
	apisecret := c.Request.FormValue("api_secret")
	toparty := c.Request.FormValue("to_party")
	phone := c.Request.FormValue("phone")
	smsparams := c.Request.FormValue("smsparams")
	author := "谭清如"
	c.JSON(http.StatusOK, gin.H{
		"method":       c.Request.Method,
		"agentid":      agentid,
		"toparty":      toparty,
		"phone":        phone,
		"author":       author,
		"authorWechat": "tanqingru",
		"debugMode":    debugMode,
	})
	// 获取并处理body数据
	Data, err := c.GetRawData()
	if err != nil {
		fmt.Println(err.Error())
	}
	//fmt.Printf("Data: %v\n", string(Data))
	//fmt.Printf("buf Type: %T\n", Data)
	//很关键
	//把读过的字节流重新放到body
	c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(Data))

	fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[INFO]开始处理消息")
	if phone == "" && corpid == "" {
		fmt.Printf(time.Now().Format("2006-01-02 15:04:05"), "[ERROR] 没有发送通道，请重新配置！\n")
	} else {
		Mess := Jsondata(Data)

		if corpid != "" && toparty != "" && apisecret != "" && agentid != 0 {
			Wechat(agentid, corpid, toparty, apisecret, Mess)
		} else {
			fmt.Printf(time.Now().Format("2006-01-02 15:04:05"), "[ERROR] 企业微信信息不全，请检查！\n")
			fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "Corpid:", corpid, "toparty:", toparty, "apisecret:", apisecret, "agentid:", agentid)
		}

		if phone != "" {
			SendSms(smsparams, phone, Mess)
		}

	}
	if *debugMode {
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "Corpid:", corpid, "toparty:", toparty, "apisecret:", apisecret, "agentid:", agentid)
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "phone:", phone, "smsparams:", smsparams, "SMSURL:", *SmsURL)
	}

	fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "[INFO]处理消息完成！")

}
func ginserver(endpoint string) {
	flag.Parse()
	if !*debugMode {
		gin.SetMode(gin.ReleaseMode)
	} else {
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "DEBUG VALUE:", *debugMode)
	}
	r := gin.Default()
	r.POST("/", postserver)
	r.Run(endpoint)
}
func main() {
	//flag.Parse()
	//fmt.Println("debug_mode: ", *debug_mode)
	//fmt.Println("port: ", *port)

	fmt.Println("GO语言学习实践产品，可能存在不严谨或适配问题，有问题请在github留言或直接联系本人")
	fmt.Println("作者：谭清如; 微信: tanqingru")
	endpoint := ":" + *port
	ginserver(endpoint)
}
